/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CargarBarrios.java
 *
 * Created on Mar 24, 2011, 5:29:45 PM
 */

package forms;

import java.awt.Cursor;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import managers.ManagerBarrios;

/**
 *
 * @author nMoncho
 */
public class CargarBarrios extends javax.swing.JFrame {

    /** Creates new form CargarBarrios */
    public CargarBarrios() {
        initComponents();
        this.setLocation(200, 200);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableBarrios = new javax.swing.JTable();
        botonAgregar = new javax.swing.JButton();
        botonBorrar = new javax.swing.JButton();
        botonCerrar = new javax.swing.JButton();
        botonGuardar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Gestionar Barrios");
        setIconImage(new ImageIcon(getClass().getResource("/resources/arrow-branch-bgr-16-ns.png")).getImage());

        jLabel1.setText("Listado de Barrios: ");

        tableBarrios.setModel(gestor.getModel());
        tableBarrios.setToolTipText("Listado de Barrios");
        tableBarrios.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tableBarriosKeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(tableBarrios);

        botonAgregar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/plus-16-ns.png"))); // NOI18N
        botonAgregar.setToolTipText("Agregar un Barrio");
        botonAgregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonAgregarActionPerformed(evt);
            }
        });

        botonBorrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/minus-16-ns.png"))); // NOI18N
        botonBorrar.setToolTipText("Borrar un Barrio");
        botonBorrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonBorrarActionPerformed(evt);
            }
        });

        botonCerrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/badge-square-cross-16-ns.png"))); // NOI18N
        botonCerrar.setText("Cerrar");
        botonCerrar.setToolTipText("Cerrar Ventana");
        botonCerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonCerrarActionPerformed(evt);
            }
        });

        botonGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/badge-square-check-16-ns.png"))); // NOI18N
        botonGuardar.setText("Guardar");
        botonGuardar.setToolTipText("Guardar Cambios");
        botonGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonGuardarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(botonGuardar)
                        .addGap(18, 18, 18)
                        .addComponent(botonCerrar))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel1)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 238, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(botonAgregar, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(botonBorrar, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {botonAgregar, botonBorrar});

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {botonCerrar, botonGuardar});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(botonAgregar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(botonBorrar))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botonCerrar)
                    .addComponent(botonGuardar))
                .addContainerGap(22, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {botonAgregar, botonBorrar});

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {botonCerrar, botonGuardar});

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botonCerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonCerrarActionPerformed
        this.dispose();
    }//GEN-LAST:event_botonCerrarActionPerformed

    private void botonBorrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonBorrarActionPerformed
        borrarFila();
    }//GEN-LAST:event_botonBorrarActionPerformed

    private void tableBarriosKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tableBarriosKeyPressed
        if(evt.getKeyCode() == KeyEvent.VK_DELETE){
            borrarFila();
        }
    }//GEN-LAST:event_tableBarriosKeyPressed

    private void botonGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonGuardarActionPerformed
        int respuesta = JOptionPane.showConfirmDialog(this, "Los cambios se haran permanentes.\nEstas seguro que deseas guardar?", "Confirmacion", JOptionPane.INFORMATION_MESSAGE+JOptionPane.YES_NO_OPTION);
        if(respuesta == JOptionPane.YES_OPTION){
            setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
            boolean exito = gestor.guardarCambios();
            setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
            if(exito)
                JOptionPane.showMessageDialog(this, "Los cambios se guardaron exitosamente", "Exito", JOptionPane.INFORMATION_MESSAGE+JOptionPane.YES_OPTION);
            else
                JOptionPane.showMessageDialog(this, "Ocurrio un error tratando de guardar los datos.\nPor favor, revisa lo datos y volve a intentarlo.", "Error", JOptionPane.ERROR_MESSAGE+JOptionPane.YES_OPTION);
        }
    }//GEN-LAST:event_botonGuardarActionPerformed

    private void botonAgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonAgregarActionPerformed
        String nombre = JOptionPane.showInputDialog(this, "Ingresa el nombre de la Barrios.", "Ingreso", JOptionPane.INFORMATION_MESSAGE+JOptionPane.YES_OPTION);
        if(nombre != null && nombre.trim().length() > 0){
            boolean exito = gestor.agregarMusica(nombre.trim());
            if(!exito)
                JOptionPane.showMessageDialog(this, "El nombre de la Barrios que ingresaste ya existe", "Error", JOptionPane.ERROR_MESSAGE+JOptionPane.YES_OPTION);
            else
                tableBarrios.updateUI();
        }else
            JOptionPane.showMessageDialog(this, "Tenes que ingresar un nombre para la Barrios", "Error", JOptionPane.ERROR_MESSAGE+JOptionPane.YES_OPTION);
    }//GEN-LAST:event_botonAgregarActionPerformed


    private void borrarFila() {
        int respuesta = JOptionPane.showConfirmDialog(this, "Estas seguro que deseas borrar este barrio?", "Confirmacion", JOptionPane.INFORMATION_MESSAGE+JOptionPane.YES_NO_OPTION);
        if(respuesta == JOptionPane.YES_OPTION){
            int[] selectedRows = this.tableBarrios.getSelectedRows();
            int row;
            for(int i=selectedRows.length-1; i>-1;i--){
                row = selectedRows[i];
                gestor.borrarMusica(row);
            }
        }

        tableBarrios.updateUI();
    }
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new CargarBarrios().setVisible(true);
            }
        });
    }

    private ManagerBarrios gestor = new ManagerBarrios();
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonAgregar;
    private javax.swing.JButton botonBorrar;
    private javax.swing.JButton botonCerrar;
    private javax.swing.JButton botonGuardar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tableBarrios;
    // End of variables declaration//GEN-END:variables
	
}